package org.itzixi.rabbitmq;


import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.itzixi.pojo.ChatMessage;
import org.itzixi.pojo.netty.ChatMsg;
import org.itzixi.service.ChatMessageService;
import org.itzixi.utils.JsonUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class RabbitMQConsumer {
    @Resource
    private ChatMessageService chatMessageService;

    @RabbitListener(queues = {RabbitMQConfig.TEST_QUEUE})
    public void watchQueue(String payload, Message message){


        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        log.info("routingKey:{}", routingKey);
        if (Objects.equals(routingKey, RabbitMQConfig.ROUTEING_KEY_IMOOC_WECHAT_MSG_SEND)){
            log.info("msg:{}", payload);
            ChatMsg chatMsg = JsonUtils.jsonToPojo(payload, ChatMsg.class);
            assert chatMsg != null;
            log.info("chatMsg:{}", chatMsg.toString());
            chatMessageService.save(chatMsg);
        }

    }

}
